<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>

<script>
var lists = [{
	id: 1,
	parent_id: 0,
	name: '海南省'
}, {
	id: 2,
	parent_id: 0,
	name: '广东省'
}, {
	id: 3,
	parent_id: 2,
	name: '广州市'
}, {
	id: 4,
	parent_id: 2,
	name: '深圳市'
}, {
	id: 5,
	parent_id: 1,
	name: '海口市'
}, {
	id: 6,
	parent_id: 1,
	name: '三亚'
}, {
	id: 7,
	parent_id: 3,
	name: '天河区'
}, {
	id: 8,
	parent_id: 3,
	name: '白云区'
}, {
	id: 9,
	parent_id: 4,
	name: '南山区'
}, {
	id: 10,
	parent_id: 4,
	name: '福田区'
}, {
	id: 11,
	parent_id: 5,
	name: '美兰区'
}]

/*
	当parent_id = 0时，时第一季
	当parent_id 存在时

 */

function recursive(data, pid) {
	let arr = [];
	
	for(let i = 0; i<data.length; i++) {
		const $v = data[i]
		if ($v.parent_id === pid) {
			let children = recursive(data, $v.id)
			if (children.length) {
				$v['children'] = children
			}
			arr.push($v)
		}
		// arr.push($v)
	}

	return arr
}

const arr = recursive(lists, 0)

console.log(arr)
</script>

</body>
</html>